package casa.depesos.app.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.adjust.sdk.Adjust;
import com.adjust.sdk.AdjustAttribution;
import com.adjust.sdk.AdjustConfig;
import com.adjust.sdk.OnAttributionChangedListener;
import com.common.base.base.AbsBaseApp;
import com.common.base.utils.SPHelper;
import com.common.base.utils.WeakHandler;
import com.mo.xi.ge.info.DaleInfoInstance;
import com.mo.xi.ge.info.DaleLoca;

import casa.depesos.app.com.activity.LoginActivity;
import casa.depesos.app.com.http.Http;
import casa.depesos.app.com.http.ReqCallback;
import casa.depesos.app.com.http.bean.IpBean;
import casa.depesos.app.com.http.req.IpReq;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;
import kotlin.jvm.functions.Function0;

public class App extends AbsBaseApp {

    static App app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {

            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                Adjust.onResume();
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {
                Adjust.onPause();
            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {

            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
        DaleInfoInstance.Companion.get(this).init(Http.HOST, BuildConfig.KEY, BuildConfig.SECRET, BuildConfig.AES);
        AdjustConfig config = new AdjustConfig(this, "eooy06fyst1c", Constant.online ? AdjustConfig.ENVIRONMENT_PRODUCTION : AdjustConfig.ENVIRONMENT_SANDBOX);
        config.setOnAttributionChangedListener(new OnAttributionChangedListener() {
            @Override
            public void onAttributionChanged(AdjustAttribution adjustAttribution) {
                SPHelper.getInstance(App.getApp()).putString("adid", adjustAttribution.adid).commit();
                Constant.adid = adjustAttribution.adid;
            }
        });
        Adjust.onCreate(config);
        Report.show("ACTION_AA_001");
        Constant.getApplyId();
        getToken();
    }

    @Override
    protected void toBk() {
        Report.show("ACTION_AA_002");
    }

    public static App getApp() {
        return app;
    }

    public void reLog() {
        SPHelper.getInstance(this).remove("usr", "apply", "white_refuse", "white_refuse_set").commit();
        Utils.reset();
        for (Activity activity : list) {
            activity.finish();
        }
        Intent i = new Intent(this, LoginActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
    }

    final WeakHandler h = new WeakHandler();

    private void getToken() {
        if (!TextUtils.isEmpty(Constant.getToken())) {
            return;
        }
        new IpReq().req(new ReqCallback<IpBean>() {
            @Override
            public void callback(boolean success, IpBean bean) {
                if (success && bean.isValid()) {
                    initToken(bean.ip.ip);
                } else {
                    h.postDelayed(() -> getToken(), 1000);
                }
            }
        });
    }

    private void initToken(String ip) {
        DaleInfoInstance.Companion.get(this).setIpAddress(new Function0<String>() {
            @Override
            public String invoke() {
                return ip;
            }
        });
        String token = DaleInfoInstance.Companion.get(this).createTokenKey(DaleLoca.LOCA_NONE);
        DaleInfoInstance.Companion.get(this).dalaInit();
        Constant.setToken(token);
    }
}
